package com.tico.editor.editors.controller;

import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.editor.editors.domain.Kafka;
import com.tico.editor.editors.domain.KafkaTopic;
import com.tico.editor.editors.param.KafkaParam;
import com.tico.editor.editors.param.KafkaTopicParam;
import com.tico.editor.editors.service.KafkaService;
import com.tico.editor.editors.service.KafkaTopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/topic")
@Slf4j
public class KafkaTopicController {
    @Autowired
    private KafkaTopicService kafkaTopicService;

    @PostMapping(value = "/save")
    public JSONObject test(@RequestBody KafkaTopic kafkaTopic, HttpServletRequest request) {
        Result res = new Result();
        try {
            kafkaTopic.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            kafkaTopic.setStatus("false");
            kafkaTopicService.insert(kafkaTopic);
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody KafkaTopicParam kafkaTopic, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = kafkaTopic.getIds().split(",");
            for(int i=0;i<str.length;i++){
                kafkaTopicService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @GetMapping(value = "/list")
    public JSONObject list(KafkaTopicParam kafkaTopicParam) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(kafkaTopicParam.getPage());
            baseQuery.setLimit(kafkaTopicParam.getLimit());
            QueryResult<KafkaTopic> pageInfo = kafkaTopicService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(KafkaTopic.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/startUsing")
    public JSONObject startUsing(@RequestBody KafkaTopic kafkaTopic, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = kafkaTopic.getIds().split(",");
            for(int i=0;i<str.length;i++){
                kafkaTopic.setId(str[i]);
                kafkaTopicService.update(kafkaTopic);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
}
